<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a href="javascript:;" @click="doSort('weigth_desc')">推荐</a>
      <a href="javascript:;" @click="doSort('')">最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <!-- 上拉刷新列表 -->
    <van-list  v-model="loading"  :finished="finished"  @load="onLoad">
      <!-- 拿到ajax 渲染页面 -->
      <article-item v-for="item in list " :key="item.id" :item ='item' ></article-item>
    </van-list>

  </div>
</template>

<script>
// 1.导入封装好的 获取ajax的面经列表页
import { getArticles } from '@/api/article.js'
import ArticleItem from '@/components/ArticleItem.vue'

export default {
  components: { ArticleItem },
  name: 'article-page',
  // 2.发送ajax请求需要的数据
  data () {
    return {
      // 1.1 当前页码
      current: 1,
      // 1.2 当前页面的列表条数
      pageSize: 10,
      // 1.3排序列表 默认最新
      sorter: '',
      // 1.4 总数量
      total: 0,
      // 1.5文章列表
      list: [],
      // 1.6 loading 数据加载小圆圈提示
      loading: false,
      // 1.6事件加载开关
      finished: false
    }
  },
  methods: {
    async onLoad () {
      // 发送ajax
      const data = await getArticles({
        current: this.current,
        pageSize: this.pageSize,
        sorter: this.sorter
      })
      console.log(data)
      this.total = data.data.total
      this.list.push(...data.data.rows)
      // 当前页码自增
      this.current++
      // 加载状态结束
      this.loading = false
      // 如果服务器数据全部加载完毕:设置finished 为true
      if (data.data.rows.length === 0) {
        this.finished = true
      }
    },
    doSort (sorter) {
      // 1.修改data数据
      this.sorter = sorter
      // 2.清空页码和原数据
      this.current = 1
      this.list = []
      // 3.发送请求
      // 手动开启动画
      this.loading = false
      this.onLoad()
    }

  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
.article-item {
  .head {
    display: flex;
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
    .con {
      flex: 1;
      overflow: hidden;
      padding-left: 10px;
      p {
        margin: 0;
        line-height: 1.5;
        &.title {
          width: 280px;
        }
        &.other {
          font-size: 10px;
          color: #999;
        }
      }
    }
  }
  .body {
    font-size: 14px;
    color: #666;
    line-height: 1.6;
    margin-top: 10px;
  }
  .foot {
    font-size: 12px;
    color: #999;
    margin-top: 10px;
  }
}
</style>
